package com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.XjrUser;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.entity.TYcodeClass;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.service.ITYcodeClassService;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard.entity.TYcodeMasterDataStandard;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard.service.ITYcodeMasterDataStandardService;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard.vo.YcodeMasterDataStandardPublishedVo;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.dto.TYcodeMasterDataStandardDetailDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.dto.SaveTYcodeMasterDataStandardDetailFormDataDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.vo.*;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.dto.TYcodeMasterDataStandardDetailListDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.entity.TYcodeMasterDataStandardDetailEnum;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.dto.TYcodeMasterDataStandardDetailEnumDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.entity.TYcodeMasterDataStandardDetail;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.service.ITYcodeMasterDataStandardDetailService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2024-07-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/t_ycode_master_data_standard_detail")
@Api(value = "主数据标准管理", tags = "主数据标准管理接口")
public class TYcodeMasterDataStandardDetailController {


	private final ITYcodeMasterDataStandardDetailService tYcodeMasterDataStandardDetailService;

	private ITYcodeMasterDataStandardService itYcodeMasterDataStandardService;

	private ITYcodeClassService itYcodeClassService;
	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tYcodeMasterDataStandardDetail")
	@SaCheckPermission(value = {"t_ycode_master_data_standard_detail:view", "t_ycode_master_data_standard_detail:modify"}, mode = SaMode.OR)
	public Response<TYcodeMasterDataStandardDetailFormDataVo> getFormData(@PathVariable String id) {
		TYcodeMasterDataStandardDetailFormDataVo formDataVo = new TYcodeMasterDataStandardDetailFormDataVo();
		// 主表数据
		TYcodeMasterDataStandardDetail tYcodeMasterDataStandardDetail = tYcodeMasterDataStandardDetailService.getById(id);
		TYcodeMasterDataStandardDetailVo tYcodeMasterDataStandardDetailVo = BeanUtil.copy(tYcodeMasterDataStandardDetail, TYcodeMasterDataStandardDetailVo.class);
		formDataVo.setTYcodeMasterDataStandardDetailVo(tYcodeMasterDataStandardDetailVo);
		// 从表数据
		List<TYcodeMasterDataStandardDetailEnumVo> tYcodeMasterDataStandardDetailEnumVoList = BeanUtil.copyList(tYcodeMasterDataStandardDetailService.getTYcodeMasterDataStandardDetailEnumByParentId(id), TYcodeMasterDataStandardDetailEnumVo.class);
		formDataVo.setTYcodeMasterDataStandardDetailEnumVoList(tYcodeMasterDataStandardDetailEnumVoList);
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tYcodeMasterDataStandardDetail")
		public Response<PageOutput<TYcodeMasterDataStandardDetailListVo>> getPageList(TYcodeMasterDataStandardDetailListDto listDto) {
		IPage<TYcodeMasterDataStandardDetail> page = tYcodeMasterDataStandardDetailService.getPageList(listDto);
		List<TYcodeMasterDataStandardDetailListVo> records = BeanUtil.copyList(page.getRecords(), TYcodeMasterDataStandardDetailListVo.class);
			// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入tYcodeMasterDataStandardDetail")
	@SaCheckPermission("t_ycode_master_data_standard_detail:add")
	public Response save(@RequestBody SaveTYcodeMasterDataStandardDetailFormDataDto formDto) {
		TYcodeMasterDataStandardDetailDto tYcodeMasterDataStandardDetailDto = formDto.getTYcodeMasterDataStandardDetailDto();
		TYcodeMasterDataStandardDetail tYcodeMasterDataStandardDetail = BeanUtil.copy(tYcodeMasterDataStandardDetailDto, TYcodeMasterDataStandardDetail.class);
		List<TYcodeMasterDataStandardDetailEnumDto> tYcodeMasterDataStandardDetailEnumDtoList = formDto.getTYcodeMasterDataStandardDetailEnumDto();
		List<TYcodeMasterDataStandardDetailEnum> tYcodeMasterDataStandardDetailEnumList = BeanUtil.copyList(tYcodeMasterDataStandardDetailEnumDtoList, TYcodeMasterDataStandardDetailEnum.class);
		boolean isSuccess = tYcodeMasterDataStandardDetailService.addTYcodeMasterDataStandardDetail(tYcodeMasterDataStandardDetail, tYcodeMasterDataStandardDetailEnumList);
		return Response.status(isSuccess);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入tYcodeMasterDataStandardDetail")
	@SaCheckPermission("t_ycode_master_data_standard_detail:modify")
	public Response update(@PathVariable String id, @RequestBody SaveTYcodeMasterDataStandardDetailFormDataDto formDto) {
		TYcodeMasterDataStandardDetailDto tYcodeMasterDataStandardDetailDto = formDto.getTYcodeMasterDataStandardDetailDto();
		TYcodeMasterDataStandardDetail tYcodeMasterDataStandardDetail = BeanUtil.copy(tYcodeMasterDataStandardDetailDto, TYcodeMasterDataStandardDetail.class);

		List<TYcodeMasterDataStandardDetailEnumDto> tYcodeMasterDataStandardDetailEnumDtoList = formDto.getTYcodeMasterDataStandardDetailEnumDto();
		List<TYcodeMasterDataStandardDetailEnum> tYcodeMasterDataStandardDetailEnumList = BeanUtil.copyList(tYcodeMasterDataStandardDetailEnumDtoList, TYcodeMasterDataStandardDetailEnum.class);
		return Response.status(tYcodeMasterDataStandardDetailService.updateTYcodeMasterDataStandardDetail(id, tYcodeMasterDataStandardDetail, tYcodeMasterDataStandardDetailEnumList));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"t_ycode_master_data_standard_detail:batchDelete", "t_ycode_master_data_standard_detail:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(tYcodeMasterDataStandardDetailService.delete(ids));
	}

	@GetMapping("/getStandardEntity")
	@ApiOperation(value = "获取主数据标准详情", notes = "standard_id")
	public Response<TYcodeMasterDataStandard> getStandardEntity(@RequestParam String standard_id) {
		TYcodeMasterDataStandard entity = itYcodeMasterDataStandardService.getById(standard_id);
		if(null == entity){			//没有数据时，获取产品分类，往数据标准新增一条数据
			TYcodeClass tYcodeClass = itYcodeClassService.getById(standard_id);
			if(null == tYcodeClass){
				Response.ok("根据id["+standard_id+"]未获取到产品分类，请联系管理员！");
			}
			entity = BeanUtil.copy(tYcodeClass,TYcodeMasterDataStandard.class);
			itYcodeMasterDataStandardService.save(entity);
		}
		return Response.ok(entity);
	}

	@GetMapping("/getStandardCompleteInfo")
	@ApiOperation(value = "获取主数据标准完整数据", notes = "standard_id")
	public Response<StandardCompleteInfoVo> getStandardCompleteInfo(@RequestParam String standard_id) {
		StandardCompleteInfoVo vo = new StandardCompleteInfoVo();
		//获取主数据标准
		TYcodeMasterDataStandard entity = itYcodeMasterDataStandardService.getById(standard_id);
		vo.setStandard(entity);
		//获取明细数据
		List<TYcodeMasterDataStandardDetail> detailList = tYcodeMasterDataStandardDetailService.getDetailListBystandardId(standard_id);
		if(CollectionUtils.isNotEmpty(detailList)){
			List<StandardCompleteInfoDetailListVo> detailListVos = new ArrayList<>();
			for(TYcodeMasterDataStandardDetail one : detailList) {
				StandardCompleteInfoDetailListVo detailListVo = BeanUtil.copy(one,StandardCompleteInfoDetailListVo.class);
				//根据明细获取明细中枚举数据
				List<TYcodeMasterDataStandardDetailEnum> enumList = tYcodeMasterDataStandardDetailService.getTYcodeMasterDataStandardDetailEnumByParentId(one.getId());
				if (CollectionUtils.isNotEmpty(detailList)) {
					detailListVo.setEnmuList(enumList);
				}
				detailListVos.add(detailListVo);
			}
			vo.setDetailList(detailListVos);
		}
		return Response.ok(vo);
	}

	/**
	 * 发布
	 */
	@PostMapping("/published")
	@ApiOperation(value = "发布", notes = "传入tYcodeMasterDataStandardDetail")
	@SaCheckPermission("t_ycode_master_data_standard_detail:published")
	public Response published(@RequestBody YcodeMasterDataStandardPublishedVo vo) {
		TYcodeMasterDataStandard entity = BeanUtil.copy(vo,TYcodeMasterDataStandard.class);
		if(1==vo.getPublished()){	//发布
			entity.setPublishDate(LocalDateTime.now());
			XjrUser currentUser = SecureUtil.getUser();
			entity.setPublisher(currentUser.getUserId());
		}else{		//撤销发布
			entity.setPublisher("");
			entity.setPublishDate(LocalDateTime.now());
		}
		return Response.ok(itYcodeMasterDataStandardService.updateById(entity));
	}
}
